package cos.gui;

import java.awt.BorderLayout;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private final String applTitle = "Tiny Lyrics Finder";
	private final LyricsPanel lyricsPanel;

	private final JMenuItem preferences = new JMenuItem("Preferences");
	private final JMenuItem showNavPanel = new JMenuItem("Show info panel");
	private final JMenuItem showTransPanel = new JMenuItem(
			"Show translation panel");
	private Icon checked;

	public MainFrame() {
		setLayout(new BorderLayout());
		setMenu();
		// set icon
		setIconImage(Toolkit.getDefaultToolkit().createImage("img/icon_64.png"));

		setTitle(applTitle);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(450, 500);
		setLocationRelativeTo(null);
		setVisible(true);
		setLayout(new BorderLayout());
		// setResizable(false);

		lyricsPanel = new LyricsPanel();
		add(lyricsPanel);

		// Tray icon
		//
		try {
			if (SystemTray.isSupported()) {
				SystemTray tray = SystemTray.getSystemTray();
				TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit()
						.createImage("img/icon_16.png"), applTitle);
				trayIcon.setImageAutoSize(true);
				trayIcon.addMouseListener(new Mouse_Handler_Tray());
				tray.add(trayIcon);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * sets menu
	 */
	public void setMenu() {

		ActionListener button_Handler = new Button_Handler();
		JMenuBar menubar = new JMenuBar();

		checked = new ImageIcon(Toolkit.getDefaultToolkit().createImage(
				"img/btn/check.png"));

		// File
		//
		JMenu file = new JMenu("File");
		List<String> fileItems = Arrays.asList("New", "Save", "Exit");
		for (String itemName : fileItems) {
			JMenuItem menuItem = new JMenuItem(itemName);
			// menuItem.addActionListener(this);
			file.add(menuItem);
		}
		// menubar.add(file);

		// Options
		//
		JMenu options = new JMenu("Options");

		showNavPanel.addActionListener(button_Handler);
		showNavPanel.setIcon(checked);
		showTransPanel.addActionListener(button_Handler);
		showTransPanel.setIcon(checked);
		preferences.addActionListener(button_Handler);
		options.add(preferences);
		options.addSeparator();
		options.add(showTransPanel);
		options.add(showNavPanel);
		menubar.add(options);

		// Help
		//
		JMenu help = new JMenu("Help");
		JMenuItem helpAbout = new JMenuItem("Help yourself!");
		// helpAbout.addActionListener(this);
		help.add(helpAbout);
		// menubar.add(help);

		setJMenuBar(menubar);

	}

	/**
	 * method, used in tray icon for show/hide main window
	 */
	private void showHideMainWindow() {
		this.setVisible(!this.isVisible());
	}

	class Button_Handler implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			// show/hide navigation panel (with winamp buttons)
			//
			if (evt.getSource() == showNavPanel) {
				boolean b = lyricsPanel.showHideInfoPanel();
				showNavPanel.setIcon(b ? null : checked);
			} else if (evt.getSource() == showTransPanel) {
				boolean b = lyricsPanel.showHideTranslationPanel();
				showTransPanel.setIcon(b ? null : checked);
			} else if (evt.getSource() == preferences) {
				new PreferencesPopup(MainFrame.this);
			}
		}
	}

	class Mouse_Handler_Tray implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
			showHideMainWindow();
		}

		@Override
		public void mousePressed(MouseEvent e) {
		}

		@Override
		public void mouseReleased(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

	}
}
